﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// Summary description for bzSponsors
/// </summary>
public class bzSponsors
{
	private dbConnection _DB = null;
    private string _TableName = "bzSponsors";
    private string _IDType = "_INT";
    private string _IDCol = "ID";
    private string _IDColParent = "";
    private string _TableChild = "";
    private string _Field = "";
    public bzSponsors()
    {
        _DB = new dbConnection();
    }
    public struct NewsPosition
    {
        private static string _Home = "1";
        public static string Home { get { return _Home; } }

        private static string _In = "0";
        public static string In { get { return _In; } }
    }
    public DataTable getTable(string Key, string Enabled,  string Page, string Limit,string Position)
    {
        SqlParameter[] p = new SqlParameter[5];
        p[0] = new SqlParameter("@Key", "%" + Key + "%");
        p[1] = new SqlParameter("@Enabled", Enabled);
        p[2] = new SqlParameter("@Page", Page);
        p[3] = new SqlParameter("@Limit", Limit);
        p[4] = new SqlParameter("@Position", Position);
        return _DB.getDataTable(_TableName + "_search", p);

    }
    public long getTotal(string Key, string Enabled,string Position)
    {
        SqlParameter[] p = new SqlParameter[3];
        p[0] = new SqlParameter("@Key", "%" + Key + "%");
        p[1] = new SqlParameter("@Enabled", Enabled);
        p[2] = new SqlParameter("@Position", Position);
        return _DB.getTotalTable(_TableName + "_getTotal", p);
    }
    public int saveOrder(string[] ID, string[] Order)
    {
        if (ID.Length != Order.Length) return 0;
        int count = 0;
        for (int i = 0; i < ID.Length; i++)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@IDCol", _IDCol);
            p[2] = new SqlParameter("@IDValue", ID[i]);
            p[3] = new SqlParameter("@Order", Order[i]);
            count += _DB.updateData("bz_saveOrder" + _IDType, p);
        }
        return count;

    }
    public int downOrder(string ID)
    {
        SqlParameter[] p = new SqlParameter[3];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        return _DB.updateData("bz_downOrder" + _IDType, p);
    }
    public int upOrder(string ID)
    {
        SqlParameter[] p = new SqlParameter[3];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        return _DB.updateData("bz_upOrder" + _IDType, p);
    }
    public DataTable getRecord(string ID)
    {
        return this.getRecord(ID, "-1");
    }
    public DataTable getRecord(string ID, string Enabled)
    {
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        p[3] = new SqlParameter("@Enabled", Enabled);
        return _DB.getDataTable("bz_getRecord" + _IDType, p);
    } 
    public int deleteRecord(string[] ID)
    {
        int count = 0;
        foreach (string str in ID)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@TableChild", _TableChild);
            p[2] = new SqlParameter("@IDCol", _IDCol);
            p[3] = new SqlParameter("@IDValue", str);
            count += _DB.updateData("bz_deleteRecord" + _IDType, p);
        }
        return count;
    }
    public int activeRecord(string[] ID, string Enabled)
    {
        int count = 0;
        foreach (string str in ID)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@IDCol", _IDCol);
            p[2] = new SqlParameter("@IDValue", str);
            p[3] = new SqlParameter("@Enabled", Enabled);
            count += _DB.updateData("bz_activeRecord" + _IDType, p);
        }
        return count;
    }   
   
    public int insertRecord(string FullName, string Link,string DesVN,string DesEN,string FileName,string Enabled)
    {
        SqlParameter[] p = new SqlParameter[6];
        p[0] = new SqlParameter("@FullName", FullName);
        p[1] = new SqlParameter("@Link", Link);
        p[2] = new SqlParameter("@DesVN", DesVN);
        p[3] = new SqlParameter("@DesEN", DesEN);
        p[4] = new SqlParameter("@FileName", FileName);
        p[5] = new SqlParameter("@Enabled", Enabled);

        return _DB.updateData(_TableName + "_insert", p);
    }
    public int updateRecord(string ID, string FullName, string Link, string DesVN, string DesEN, string FileName, string Enabled)
    {
        SqlParameter[] p = new SqlParameter[7];
        p[0] = new SqlParameter("@ID", ID);
        p[1] = new SqlParameter("@FullName", FullName);
        p[2] = new SqlParameter("@Link", Link);
        p[3] = new SqlParameter("@DesVN", DesVN);
        p[4] = new SqlParameter("@DesEN", DesEN);
        p[5] = new SqlParameter("@FileName", FileName);
        p[6] = new SqlParameter("@Enabled", Enabled);
        return _DB.updateData(_TableName + "_update", p);
    }

  public DataTable getTableOther(string ID, string Page, string Limit,string Position)
    {
        SqlParameter[] p = new SqlParameter[3];
        p[0] = new SqlParameter("@ID", ID);
        p[1] = new SqlParameter("@Page", Page);
        p[2] = new SqlParameter("@Limit", Limit);
        p[3] = new SqlParameter("@Position", Position);
        return _DB.getDataTable(_TableName + "_other", p);
    }
  public int changeHome(string ID)
  {
      SqlParameter[] p = new SqlParameter[4];
      p[0] = new SqlParameter("@TableName", _TableName);
      p[1] = new SqlParameter("@IDCol", _IDCol);
      p[2] = new SqlParameter("@IDValue", ID);
      p[3] = new SqlParameter("@IDColActive", "Position");
      return _DB.updateData("bz_updateRecordStatus", p);
  }
}
